package com.cloud.wms.widget.dialog;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

/**
 * @Author： Chen
 * @Date： 2021/5/23
 * @Desc：
 */
public class AlertDialogBuilder extends MaterialAlertDialogBuilder {
    private Context mContext;

    public AlertDialogBuilder(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public AlertDialogBuilder(@NonNull Context context, int overrideThemeResId) {
        super(context, overrideThemeResId);
        mContext = context;
    }

    @Override
    public AlertDialog show() {
        AlertDialog dialog = super.show();
        //放在show()之后，不然有些属性是没有效果的，比如height和width
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            DisplayMetrics display = mContext.getResources().getDisplayMetrics();
            WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes(); // 获取对话框当前的参数值
            // 设置宽度
            layoutParams.width = (int) (display.widthPixels * 0.95); // 宽度设置为屏幕的0.95
            layoutParams.gravity = Gravity.CENTER;//设置位置
            //layoutParams.alpha = 0.8f;//设置透明度
            dialogWindow.setAttributes(layoutParams);
        }
        return dialog;
    }
}
